#include <bits/stdc++.h>
#define _for(i, a, b)  for (int i = (a); i <= (b); i ++ )
#define int long long
using namespace std;
const int N = 4e5 + 5, V = 100;
int l, r, ans, cur, a[N]; set<int> s, t;
struct Heap {
	priority_queue<int, vector<int>, greater<int> > q1, q2; int s = 0, sz = 0;
	inline int top() { while (q2.size() && q1.top() == q2.top())  q1.pop(), q2.pop(); return q1.top(); }
	inline void ins(int x) { q1.push(x), s += x, sz ++ ; }
	inline void del(int x) { q2.push(x), s -= x, sz -- ; }
	inline int calc() { return sz >= 2 ? s - 2 * top() : 0; }
} heap;
namespace brute_force {
	int x, sz = 0;
	inline void solve(int T) {
		while (T -- ) {
			cin >> x, ans = l = r = cur = 0;
			if (s.find(x) != s.end())  s.erase(x), sz -- ;
			else  s.insert(x), sz ++ ; t = s;
			_for (i, 1, sz)  a[i] = * t.begin(), t.erase(a[i]);
			if (sz <= 1)  cout << "0\n";
			else {
				_for (i, 1, sz - 1) {
					l += a[i], r += a[sz - i + 1];
					if (cur - 1 < l)  ans += r - l;
					else  ans += r - cur; cur = r;
				} cout << ans << "\n";
			}
		}
	}
}
signed main() {
	freopen("kte.in", "r", stdin);
	freopen("kte.out", "w", stdout);
	ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T, x; cin >> T;
	if (T <= 1000)  return brute_force :: solve(T), 0;
	while (T -- ) {
		cin >> x;
		if (s.find(x) != s.end())  s.erase(x), heap.del(x);
		else  s.insert(x), heap.ins(x);
		if (heap.sz >= V)  cout << heap.calc() << "\n";
		else {
			t = s, l = r = ans = cur = 0;
			_for (i, 1, heap.sz)  a[i] = * t.begin(), t.erase(a[i]);
			if (heap.sz <= 1)  cout << "0\n";
			else {
				_for (i, 1, heap.sz - 1) {
					l += a[i], r += a[heap.sz - i + 1];
					if (cur - 1 < l)  ans += r - l;
					else  ans += r - cur; cur = r;
				} cout << ans << "\n";
			}
		}
	}
	return 0;
}

